package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.*;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RoadmapBigTypeServiceImpl implements RoadmapBigTypeService {

    @Autowired
    private RoadmapBigTypeDao roadmapBigTypeDao;

    @Autowired
    private RoadmapSmallTypeDao roadmapSmallTypeDao;

    @Autowired
    private RoadmapParamDao roadmapParamDao;

    @Autowired
    private RoadmapH3cDao roadmapH3cDao;

    @Autowired
    private RoadmapCmDao roadmapCmDao;

    @Autowired
    private PcbaCompanyDao pcbaCompanyDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private CoreAlgorithmService coreAlgorithmService;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "Roadmap大类";

    @Override
    public List<RoadmapBigType> getAllRoadmapBigType(String searchValue) {
        List<RoadmapBigType> list = roadmapBigTypeDao.getAllRoadmapBigType(searchValue);
        for (RoadmapBigType roadmapBigType : list) {
            roadmapBigType.setRoadmapSmallTypeList(roadmapSmallTypeDao.getAllRoadmapSmallType(null, roadmapBigType.getRoadmapBigTypeId()));
        }
        return list;
    }

    @Override
    public Result deleteRoadmapBigType(Integer roadmapBigTypeId) {
        try {
            if (roadmapBigTypeId == null || "".equals(roadmapBigTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数roadmapBigTypeId", "Miss Param roadmapBigTypeId", null);
            }
            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeleteRoadmapBigType(roadmapBigTypeId);
            if (stringIntegerMap.get("roadmapSmallTypeNum") > 0 || stringIntegerMap.get("roadmapParamNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("roadmapSmallTypeNum") + "处关联小类，"
                                + stringIntegerMap.get("roadmapParamNum") + "处关联roadmap能力项",
                        "Forbidden Delete", null);
            }

            String userId = UserHeaderHolder.getUserId();
            RoadmapBigType oldRoadmapBigType = roadmapBigTypeDao.getRoadmapBigTypeById(roadmapBigTypeId);

            Integer integer = roadmapBigTypeDao.deleteRoadmapBigType(roadmapBigTypeId);
            if (integer > 0) {

                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldRoadmapBigType), null));

                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result addOrUpdateRoadmapBigType(String mode, RoadmapBigType roadmapBigType) {
        try {
            if (roadmapBigType == null) {
                return ResultUtil.error(500, "缺少参数roadmapBigType", "Miss Param roadmapBigType", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (roadmapBigType.getRoadmapBigTypeId() == null || "".equals(roadmapBigType.getRoadmapBigTypeId().toString())) {
                    return ResultUtil.error(500, "缺少参数roadmapBigTypeId", "Miss Param roadmapBigTypeId", null);
                }

                RoadmapBigType oldRoadmapBigType = roadmapBigTypeDao.getRoadmapBigTypeById(roadmapBigType.getRoadmapBigTypeId());

                roadmapBigType.setUpdateTime(new Date());
                Integer integer = roadmapBigTypeDao.updateRoadmapBigType(roadmapBigType);
                if (integer > 0) {

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldRoadmapBigType), JSONObject.toJSONString(roadmapBigType)));

                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {
                roadmapBigType.setCreateTime(new Date());
                Integer integer = roadmapBigTypeDao.addRoadmapBigType(roadmapBigType);
                if (integer > 0) {

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(roadmapBigType)));

                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result getH3cRoadmap(Integer year, Integer versionY, String versionN) {
        try {
            if (year == null || "".equals(year.toString())) {
                return ResultUtil.error(500, "缺少参数year", "Miss Param year", null);
            }
            if (versionY == null || "".equals(versionY.toString())) {
                return ResultUtil.error(500, "缺少参数versionY", "Miss Param versionY", null);
            }
            if (versionN == null || "".equals(versionN)) {
                return ResultUtil.error(500, "缺少参数versionN", "Miss Param versionN", null);
            }
            List<RoadmapBigType> list = roadmapBigTypeDao.getAllRoadmapBigType(null);
            for (RoadmapBigType roadmapBigType : list) {
                List<RoadmapSmallType> allRoadmapSmallType = roadmapSmallTypeDao.getAllRoadmapSmallType(null, roadmapBigType.getRoadmapBigTypeId());
                for (RoadmapSmallType roadmapSmallType : allRoadmapSmallType) {
                    List<RoadmapParam> allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, roadmapBigType.getRoadmapBigTypeId(), roadmapSmallType.getRoadmapSmallTypeId());
                    for (RoadmapParam roadmapParam : allRoadmapParam) {
                        // 循环匹配 h3c能力
                        List<RoadmapH3c> allRoadmapH3c = roadmapH3cDao.getAllRoadmapH3c(year, versionY, versionN);
                        for (int a = 0; a < allRoadmapH3c.size(); a++) {
                            if (allRoadmapH3c.get(a).getRoadmapH3cParamKey().equals(roadmapParam.getRoadmapParamKey())) {
                                roadmapParam.setValue1(allRoadmapH3c.get(a).getRoadmapH3cParamValue1());
                                roadmapParam.setValue2(allRoadmapH3c.get(a).getRoadmapH3cParamValue2());
                                roadmapParam.setValue3(allRoadmapH3c.get(a).getRoadmapH3cParamValue3());
                                break;
                            }
                        }
                    }
                    roadmapSmallType.setRoadmapParamList(allRoadmapParam);
                }
                roadmapBigType.setRoadmapSmallTypeList(allRoadmapSmallType);
            }
            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }


    @Override
    public Result getH3cThreeYearRoadmap(Integer versionY, String versionN) {
        try {
            if (versionY == null || "".equals(versionY.toString())) {
                return ResultUtil.error(500, "缺少参数versionY", "Miss Param versionY", null);
            }
            if (versionN == null || "".equals(versionN)) {
                return ResultUtil.error(500, "缺少参数versionN", "Miss Param versionN", null);
            }
            // 今年
            Calendar cal = Calendar.getInstance();
            Integer year = cal.get(Calendar.YEAR);
            //明年
            Integer yearAdd1 = year + 1;
            //后年
            Integer yearAdd2 = year + 2;

            List<RoadmapBigType> list = roadmapBigTypeDao.getAllRoadmapBigType(null);
            for (RoadmapBigType roadmapBigType : list) {
                List<RoadmapSmallType> allRoadmapSmallType = roadmapSmallTypeDao.getAllRoadmapSmallType(null, roadmapBigType.getRoadmapBigTypeId());
                for (RoadmapSmallType roadmapSmallType : allRoadmapSmallType) {
                    List<RoadmapParam> allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, roadmapBigType.getRoadmapBigTypeId(), roadmapSmallType.getRoadmapSmallTypeId());
                    for (RoadmapParam roadmapParam : allRoadmapParam) {
                        RoadmapH3c roadmapH3c = roadmapH3cDao.getRoadmapH3cByYearAndKey(year.toString(), roadmapParam.getRoadmapParamKey(), versionY, versionN);
                        if (roadmapH3c != null) {
                            roadmapParam.setValueH3C1(roadmapH3c.getRoadmapH3cParamValue1());
                            roadmapParam.setValueH3C2(roadmapH3c.getRoadmapH3cParamValue2());
                            roadmapParam.setValueH3C3(roadmapH3c.getRoadmapH3cParamValue3());
                        }
                        RoadmapH3c roadmapH3c2 = roadmapH3cDao.getRoadmapH3cByYearAndKey(yearAdd1.toString(), roadmapParam.getRoadmapParamKey(), versionY, versionN);
                        if (roadmapH3c2 != null) {
                            roadmapParam.setValueH3C1(roadmapH3c2.getRoadmapH3cParamValue1());
                            roadmapParam.setValueH3C2(roadmapH3c2.getRoadmapH3cParamValue2());
                            roadmapParam.setValueH3C3(roadmapH3c2.getRoadmapH3cParamValue3());
                        }
                        RoadmapH3c roadmapH3c3 = roadmapH3cDao.getRoadmapH3cByYearAndKey(yearAdd2.toString(), roadmapParam.getRoadmapParamKey(), versionY, versionN);
                        if (roadmapH3c3 != null) {
                            roadmapParam.setValueH3C1(roadmapH3c3.getRoadmapH3cParamValue1());
                            roadmapParam.setValueH3C2(roadmapH3c3.getRoadmapH3cParamValue2());
                            roadmapParam.setValueH3C3(roadmapH3c3.getRoadmapH3cParamValue3());
                        }
                    }
                    roadmapSmallType.setRoadmapParamList(allRoadmapParam);
                }
                roadmapBigType.setRoadmapSmallTypeList(allRoadmapSmallType);
            }
            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }


    @Override
    public Result getCmRoadmapByCompany(Integer pcbaCompanyId, Integer versionY, String versionN) {
        try {
            if (pcbaCompanyId == null || "".equals(pcbaCompanyId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
            }
            if (versionY == null || "".equals(versionY.toString())) {
                return ResultUtil.error(500, "缺少参数versionY", "Miss Param versionY", null);
            }
            if (versionN == null || "".equals(versionN)) {
                return ResultUtil.error(500, "缺少参数versionN", "Miss Param versionN", null);
            }
            List<RoadmapBigType> list = roadmapBigTypeDao.getAllRoadmapBigType(null);
            for (RoadmapBigType roadmapBigType : list) {
                List<RoadmapSmallType> allRoadmapSmallType = roadmapSmallTypeDao.getAllRoadmapSmallType(null, roadmapBigType.getRoadmapBigTypeId());
                for (RoadmapSmallType roadmapSmallType : allRoadmapSmallType) {
                    List<RoadmapParam> allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, roadmapBigType.getRoadmapBigTypeId(), roadmapSmallType.getRoadmapSmallTypeId());
                    for (RoadmapParam roadmapParam : allRoadmapParam) {
                        // 循环匹配 cm 能力
                        // value1 需要取自规则，不能通过查询数据库
                        String finalValue = "";
                        Result result = coreAlgorithmService.calcRoadmapCmParamValue1(pcbaCompanyId, roadmapParam.getRoadmapParamKey());
                        if (result.getCode() == 200) {
                            finalValue = result.getData().toString();
                        }
                        roadmapParam.setValue1(finalValue);

                        // 根据 key 和 工厂id 查询
                        RoadmapCm roadmapCm = roadmapCmDao.getRoadmapCmByCompanyAndKey(pcbaCompanyId, roadmapParam.getRoadmapParamKey(), versionY, versionN);
                        if (roadmapCm != null) {
                            roadmapParam.setValue2(roadmapCm.getRoadmapCmParamValue2());
                            roadmapParam.setValue3(roadmapCm.getRoadmapCmParamValue3());
                            roadmapParam.setCauses1(roadmapCm.getAbilityMismatchCauses1());
                            roadmapParam.setPlan1(roadmapCm.getAbilityImprovePlan1());
                            roadmapParam.setCauses2(roadmapCm.getAbilityMismatchCauses2());
                            roadmapParam.setPlan2(roadmapCm.getAbilityImprovePlan2());
                            roadmapParam.setCauses3(roadmapCm.getAbilityMismatchCauses3());
                            roadmapParam.setPlan3(roadmapCm.getAbilityImprovePlan3());
                        }

                        /*List<RoadmapCm> allRoadmapCm = roadmapCmDao.getAllRoadmapCMByCompany(pcbaCompanyId);
                        for(int a=0;a<allRoadmapCm.size();a++){
                            if(allRoadmapCm.get(a).getRoadmapCmParamKey().equals(roadmapParam.getRoadmapParamKey())){
                                roadmapParam.setValue2(allRoadmapCm.get(a).getRoadmapCmParamValue2());
                                roadmapParam.setValue3(allRoadmapCm.get(a).getRoadmapCmParamValue3());
                                roadmapParam.setCauses(allRoadmapCm.get(a).getAbilityMismatchCauses());
                                roadmapParam.setPlan(allRoadmapCm.get(a).getAbilityImprovePlan());
                                break;
                            }
                        }*/
                    }
                    roadmapSmallType.setRoadmapParamList(allRoadmapParam);
                }
                roadmapBigType.setRoadmapSmallTypeList(allRoadmapSmallType);
            }
            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result getCmRoadmapByCompanyIdsAndCalcCapacityIsMatch(String pcbaCompanyIdsStr, Integer year, Integer versionY, String versionN) {
        try {
            if (pcbaCompanyIdsStr == null || "".equals(pcbaCompanyIdsStr)) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyIdsStr", "Miss Param pcbaCompanyIdsStr", null);
            }
            if (year == null || "".equals(year.toString())) {
                return ResultUtil.error(500, "缺少参数year", "Miss Param year", null);
            }
            if (versionY == null || "".equals(versionY.toString())) {
                return ResultUtil.error(500, "缺少参数versionY", "Miss Param versionY", null);
            }
            if (versionN == null || "".equals(versionN)) {
                return ResultUtil.error(500, "缺少参数versionN", "Miss Param versionN", null);
            }
            String[] companyIdsArr = pcbaCompanyIdsStr.split(",");
            List<List<RoadmapParam>> finalList = new ArrayList<>();

            List<RoadmapParam> allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, null, null);
            // 先得到h3c的能力
            for (RoadmapParam roadmapParam : allRoadmapParam) {
                RoadmapBigType roadmapBigTypeById = roadmapBigTypeDao.getRoadmapBigTypeById(roadmapParam.getRoadmapBigTypeId());
                if (roadmapBigTypeById != null) {
                    roadmapParam.setRoadmapBigTypeName(roadmapBigTypeById.getRoadmapBigTypeName());
                }
                RoadmapSmallType roadmapSmallTypeById = roadmapSmallTypeDao.getRoadmapSmallTypeById(roadmapParam.getRoadmapSmallTypeId());
                if (roadmapSmallTypeById != null) {
                    roadmapParam.setRoadmapSmallTypeName(roadmapSmallTypeById.getRoadmapSmallTypeName());
                }

                RoadmapH3c roadmapH3c = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year, versionY, versionN);
                if (roadmapH3c != null) {
                    roadmapParam.setValue1(roadmapH3c.getRoadmapH3cParamValue1());
                    roadmapParam.setValue2(roadmapH3c.getRoadmapH3cParamValue2());
                    roadmapParam.setValue3(roadmapH3c.getRoadmapH3cParamValue3());
                }

                /*List<RoadmapH3c> allRoadmapH3c = roadmapH3cDao.getAllRoadmapH3c();
                for(int a=0;a<allRoadmapH3c.size();a++){
                    if(allRoadmapH3c.get(a).getRoadmapH3cParamKey().equals(roadmapParam.getRoadmapParamKey())){
                        roadmapParam.setValue1(allRoadmapH3c.get(a).getRoadmapH3cParamValue1());
                        roadmapParam.setValue2(allRoadmapH3c.get(a).getRoadmapH3cParamValue2());
                        roadmapParam.setValue3(allRoadmapH3c.get(a).getRoadmapH3cParamValue3());
                        break;
                    }
                }*/
            }
            finalList.add(allRoadmapParam);

            for (String s : companyIdsArr) {
                if (!CommonUtils.isNumeric(s)) {
                    return ResultUtil.error(500, "错误参数pcbaCompanyIdsStr", "Error Param pcbaCompanyIdsStr", null);
                }
                allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, null, null);
                PcbaCompany pcbaCompany = pcbaCompanyDao.getPcbaCompanyById(Integer.parseInt(s));
                for (RoadmapParam roadmapParam : allRoadmapParam) {
                    roadmapParam.setCompanyName(pcbaCompany.getPcbaCompanyNameCn());
                    //roadmapParam.setRoadmapBigType(roadmapBigTypeDao.getRoadmapBigTypeById(roadmapParam.getRoadmapBigTypeId()));
                    //roadmapParam.setRoadmapSmallType(roadmapSmallTypeDao.getRoadmapSmallTypeById(roadmapParam.getRoadmapSmallTypeId()));
                    // 循环匹配 cm 能力
                    // value1 需要取自规则，不能通过查询数据库
                    String finalValue = "";
                    Result result = coreAlgorithmService.calcRoadmapCmParamValue1(Integer.parseInt(s), roadmapParam.getRoadmapParamKey());
                    if (result.getCode() == 200) {
                        finalValue = result.getData().toString();
                    }
                    roadmapParam.setValue1(finalValue);

                    RoadmapH3c roadmapH3c = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year, versionY, versionN);
                    String finalIsMatch1 = "否";
                    if (roadmapH3c != null) {
                        Result result2 = coreAlgorithmService.calcRoadmapCmIsMatchH3c(roadmapParam.getValueAlgorithmType(), roadmapParam.getRuleAlgorithmType(), roadmapH3c.getRoadmapH3cParamValue1(), finalValue);
                        if (result2.getCode() == 200) {
                            finalIsMatch1 = result2.getData().toString();
                        }
                    }

                    RoadmapCm roadmapCm = roadmapCmDao.getRoadmapCmByCompanyAndKey(Integer.parseInt(s), roadmapParam.getRoadmapParamKey(), versionY, versionN);
                    if (roadmapCm != null) {
                        roadmapParam.setValue2(roadmapCm.getRoadmapCmParamValue2());
                        roadmapParam.setValue3(roadmapCm.getRoadmapCmParamValue3());
                        roadmapParam.setCauses1(roadmapCm.getAbilityMismatchCauses1());
                        roadmapParam.setPlan1(roadmapCm.getAbilityImprovePlan1());
                        roadmapParam.setCauses2(roadmapCm.getAbilityMismatchCauses2());
                        roadmapParam.setPlan2(roadmapCm.getAbilityImprovePlan2());
                        roadmapParam.setCauses3(roadmapCm.getAbilityMismatchCauses3());
                        roadmapParam.setPlan3(roadmapCm.getAbilityImprovePlan3());
                        roadmapParam.setIsMatch1(finalIsMatch1);
                    } else {
                        roadmapParam.setIsMatch1(finalIsMatch1);
                    }

                    /*List<RoadmapCm> allRoadmapCm = roadmapCmDao.getAllRoadmapCMByCompany(Integer.parseInt(s));
                    for(int a=0;a<allRoadmapCm.size();a++){
                        if(allRoadmapCm.get(a).getRoadmapCmParamKey().equals(roadmapParam.getRoadmapParamKey())){

                            RoadmapH3c roadmapH3c = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey());
                            String finalIsMatch = "否";
                            if(roadmapH3c != null){
                                Result result2 = coreAlgorithmService.calcRoadmapCmIsMatchH3c(roadmapParam.getValueAlgorithmType(),roadmapParam.getRuleAlgorithmType(),roadmapH3c.getRoadmapH3cParamValue1(),finalValue);
                                if(result2.getCode() == 200){
                                    finalIsMatch = result2.getData().toString();
                                }
                            }

                            roadmapParam.setValue2(allRoadmapCm.get(a).getRoadmapCmParamValue2());
                            roadmapParam.setValue3(allRoadmapCm.get(a).getRoadmapCmParamValue3());
                            roadmapParam.setCauses(allRoadmapCm.get(a).getAbilityMismatchCauses());
                            roadmapParam.setPlan(allRoadmapCm.get(a).getAbilityImprovePlan());
                            roadmapParam.setIsMatch(finalIsMatch);
                            break;
                        }
                    }*/
                }
                finalList.add(allRoadmapParam);
            }
            return ResultUtil.success(finalList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
